﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class BanGun : Form
    {
        DBHelper db;
        string bgCode = "";
        public BanGun(string pCode)
        {
            InitializeComponent();
            db = new DBHelper();
            bgCode = pCode;
        }

        private void BanGun_Load(object sender, EventArgs e)
        {
            try
            {
                loadPrintTemplte();
                loadInfo();
                loadPrint();
                tbNumber.Text = "1";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 打印机列表
        /// </summary>
        private void loadPrint()
        {
            PrintDocument print = new PrintDocument();
            string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("value");
            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                DataRow r = dt.NewRow();
                r["name"] = sPrint;
                r["value"] = sPrint;
                dt.Rows.Add(r);
            }
            cbPrint.DataSource = dt;
            cbPrint.ValueMember = "name";
            cbPrint.DisplayMember = "name";
            cbPrint.SelectedValue = sDefault;
        }
        /// <summary>
        /// 根据id获取版辊相关信息
        /// </summary>
        public void loadInfo()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select pCode,customName,proName,spec,banhao,zbcjName,banchang,");
            sb.Append("banzhou,banzhi,gongyongban,banchang ");
            sb.Append("from V_BanGun_Info where pCode='" + bgCode + "'");
            var table = db.getData(sb.ToString(), "");
            txt_code.Text = table.Rows[0]["pCode"].ToString();
            txt_name.Text = table.Rows[0]["customName"].ToString();
            txt_proname.Text = table.Rows[0]["proName"].ToString();
            txt_spec.Text = table.Rows[0]["spec"].ToString();
            txt_bh.Text = table.Rows[0]["banhao"].ToString();
            txt_zbcj.Text = table.Rows[0]["zbcjName"].ToString();
            txt_bc.Text = table.Rows[0]["banchang"].ToString();
            txt_bz.Text = table.Rows[0]["banzhou"].ToString();
            txt_bzs.Text = table.Rows[0]["banzhi"].ToString();
            txt_gyb.Text = table.Rows[0]["gongyongban"].ToString();
            txt_cf.Text = table.Rows[0]["banchang"].ToString();
        }

        /// <summary>
        /// 打印模板
        /// </summary>
        private void loadPrintTemplte()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select lbCode,lbName from bar_PrintTemplates_list where status=1");
            // sb.Append(" and lType='" + lType + "'");
            var table = db.getData(sb.ToString(), "");
            cbTemplate.DataSource = table;
            cbTemplate.ValueMember = "lbCode";
            cbTemplate.DisplayMember = "lbName";
        }

        /// <summary>
        /// 版辊条码打印
        /// </summary>
        private void printBanGun()
        {
            var table = CommonMethods.BanGunPrintTable();
            #region 填充表格数据
            for (int i = 0; i < int.Parse(tbNumber.Text); i++)
            {
                var r = table.NewRow();
                r["pCode"] = txt_code.Text;
                r["customName"] = txt_name.Text;
                r["proName"] = txt_proname.Text;
                r["spec"] = txt_spec.Text;
                r["banhao"] = txt_bh.Text;
                r["zbcjName"] = txt_zbcj.Text;
                r["banchang"] = txt_bc.Text;
                r["banzhou"] = txt_bz.Text;
                r["banzhi"] = txt_bzs.Text;
                r["gongyongban"] = txt_gyb.Text;
                table.Rows.Add(r);
            }
           
            #endregion
            string info = "";
            var pResult = CommonMethods.DoPrint(table, cbTemplate.Text, cbPrint.Text, ref info);
            if (!pResult)
            {
                MessageBox.Show("条码打印失败：" + info, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                printBanGun();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

    }
}
